// pages/search/search.js
const app = getApp()
var imgBaseUrl = app.globalData.imgBaseUrl;
var baseUrl = app.globalData.baseUrl;
var imgPreUrl = app.globalData.imgPreUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    imgBaseUrl: imgBaseUrl,
    baseUrl: baseUrl,
    imgPreUrl: imgPreUrl,
    resList: [],
    page:1,
    key:'',
    readMore: true,
    loadHidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var isLogin=app.isLogin();
    this.setData({
      isLogin:isLogin
    })
    that.getEveryCar();
  },
  getEveryCar: function (addcart, e) {
    var that = this;
    app.func.postReq('/text/index.php/home/shopcar/everycar', {}, function (res) {
      that.dealCartGoods(res.pro);
      if (addcart == 1) {
        that.addCart(e);
      }
    })
  },
  dealCartGoods: function(goods){

      var data = {};
      goods.forEach(o=>{

          if(o.goods_type){

              data[o.goods_id+o.goods_type] = o.num;
          }else{

              data[o.goods_id] = o.num;
          }
      });

      console.log(data);
      this.setData({
          pro: data
      });
  },
  bindSearch:function(e){

    console.log(12321321);
    var that=this;
    var key=e.detail.value;
    app.func.postReq("/text/index.php/home/product/prosearch",{title:key,p:1},function(res){
        console.log(res.product);
        if(res.product==null){
          wx.showToast({
            title: '暂无此商品',
            icon:"none"
          })
        }else{
          wx.pageScrollTo({
            scrollTop: 0,
            duration:0
          })
          that.setData({
            page:2,
            resList: res.product,
            nodata:false,
            readMore:true
          })
        }
    });
  },
  bindInput:function(e){
    this.setData({
      key:e.detail.value
    })
  },
  prevent: function () {
    //阻止冒泡
  },
  getUserInfo: function (e) {
    var that = this;
    var data = {};
    if (e.detail.errMsg == "getUserInfo:ok") {
      that.login(e.detail.userInfo, e);
    }
  },
  login: function (data, e) {
    var that = this;
    wx.login({
      success: res => {
        data.code = res.code;
        app.func.postReq('/text/index.php/home/login/adduser', data, function (res) {
          if (res) {
            wx.setStorageSync('hhh', res.users.id)
            that.setData({ isLogin: 1 })
            that.getEveryCar(1, e);
          }
        })
      }
    })
  },
  addCart: function (e) {
    const that = this;
    var id = e.currentTarget.dataset.id;
    var { id, num, cartnum ,price,user_price,sfp_price,gtype} = e.currentTarget.dataset;
    gtype = gtype ? gtype : '';

    if (parseFloat(cartnum) >= parseFloat(num)) {
      wx.showToast({
        title: '库存不足!',
        icon: 'none'
      })
      return;
    }

    if(sfp_price){

        price = sfp_price;
    // }else if(that.data.user.grade > 0 && user_price > 0){
    }else if( user_price > 0){


        price = user_price;
    }

    app.func.postReq('/text/index.php/home/shopcar/addscar', {pid:id,num:1,goods_price:price,goods_type:gtype}, function (res) {

      if(res > 0){

          var pro = that.data.pro ? that.data.pro : {};
          if(gtype){

              pro[id+gtype] = parseInt(res);
          }else{

              pro[id] = parseInt(res);
          }
      }

      console.group('addscar');
      console.log(pro);
      console.groupEnd(); 
      
      that.setData({          
          pro:pro
      });
    })
  },
  jianCart: function (e) {
    const that = this;
    var id = e.currentTarget.dataset.id;
    var gtype = e.currentTarget.dataset.gtype ? e.currentTarget.dataset.gtype : '';
    var pro = that.data.pro;
    if (parseInt(pro[id+gtype])==0){
      return;
    }
    app.func.postReq('/text/index.php/home/shopcar/minuescar', { pid: id, num: 1 ,goods_type:gtype}, function (res) {

      if(pro){

          if (parseInt(pro[id+gtype])==0){
              return;
          }

          pro[id+gtype] = res;
          that.setData({
            pro: pro
          });
          console.group('jian');
              console.log(pro);
          console.groupEnd();
      }else{

          return;
      }
    })
  },
  onReachBottom: function () {
    var that=this;
    if(that.data.readMore){
      that.setData({
        loadHidden: true
      })
      var resList=that.data.resList;
      app.func.postReq("/text/index.php/home/product/prosearch", { title: that.data.key, p: that.data.page }, function (res) {
        var readMore, nodata;
        if (res.product) {
          readMore = res.product.length < 20 ? false : true;
          resList = resList.concat(res.product);
          nodata = resList.length > 4 && res.product.length < 20 ? true : false;
        } else {
          readMore = false;
          nodata = resList.length > 4?true:false;
        }
        that.setData({
          resList: resList,
          page: ++that.data.page,
          nodata: nodata,
          readMore: readMore,
          loadHidden: false
        })
      });
    }
  }
})